<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class LegelAdvicesController extends AppController {

	const CAT_TYPE = "'legal_advice'";
	/**
	* Components
	* @var array
	*/
	public $components = array(
			'Paginator',
			'Upload',
			'Common',
	);
	public $helper = array('Utilities');
	public $uses = array('LegelAdvices', 'Category', 'Support');
	private $categorymulti = array();
	private $stt = -1;

	public function beforeFilter(){
		parent::beforeFilter();
		$category_type = Configure::read('category_type');
		$this->set('category_type', $category_type);
	}

	public function index(){

		//get all static page
		$conditions = array('del_flg' => 0);

		$paginate = array(
				'fields' => '*',
				'limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'conditions' => $conditions,
				'order' => array(
					'last_update' => 'DESC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listNews = $this->Paginator->paginate('LegelAdvices');
		//force view
		$this->set(array(
				'datas' => $listNews,
				'page_number_limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'title_for_layout' => $this->moduleAdmin['legal_advice']['index'],
		));
	}

	/**
	 * Add news
	 *
	 */
	public function add(){
		//get category parent
		$listCategory = $this->Category->getListCategoryType(self::CAT_TYPE);
		$categories = array();
		foreach ($listCategory as $key => $category){
			$categorys = $category['categories_multi'];
			$categories[$categorys['cat_parent_id']][$categorys['id']] = $categorys;
		}

		$this->_sortLevel($categories);

		//lay nguoi tu van
		$supports   = $this->Support->getAllSupport();
		$arrSupport = array();
		foreach ($supports as $values) {
			foreach ($values as $support) {
				$arrSupport[$support['id']] = $support['name'];
			}
		}

		if ($this->request->is('post')) {
			$this->LegelAdvices->create();

			$params = $this->request->data;

			$params['LegelAdvices']['create_time'] = time();
			$params['LegelAdvices']['last_update'] = time();

			//lay thong tin nguoi support
			$supportId = $params['LegelAdvices']['support_id'];

			foreach ($supports as $values) {
				foreach ($values as $support) {
					if($support['id'] == $supportId) {
						$params['LegelAdvices']['support_name']  = $support['name'];
						$params['LegelAdvices']['support_skype'] = $support['skype'];
						$params['LegelAdvices']['support_yahoo'] = $support['yahoo'];
						$params['LegelAdvices']['support_phone'] = $support['hotline'];
					}
				}
			}

			if ($this->LegelAdvices->save($params)) {
				$this->Session->setFlash(__('Success'), 'success');
				$this->redirect(array('controller' => 'LegelAdvices', 'action' => 'index'));
			} else {
				$this->Session->setFlash(__('Save could not be saved. Please, try again'), 'error');
			}
		}

		$this->set('supports', $arrSupport);
		$this->set('listCategory', $this->categorymulti);
		$this->set('title_for_layout', $this->moduleAdmin['legal_advice']['add']);

	}

	/**
	 * Edit news
	 *
	 * @param string $id
	 */
	public function edit($id = null){
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}

		//get detail
		$detail = $this->LegelAdvices->findById($id);
		if (!$detail) {
			throw new NotFoundException(__('Invalid post'));
		}

		//get category parent
		$listCategory = $this->Category->getListCategoryType(self::CAT_TYPE);
		$categories = array();
		foreach ($listCategory as $key => $category){
			$categorys = $category['categories_multi'];
			$categories[$categorys['cat_parent_id']][$categorys['id']] = $categorys;
		}

		$this->_sortLevel($categories);

		//lay nguoi tu van
		$supports   = $this->Support->getAllSupport();
		$arrSupport = array();
		foreach ($supports as $values) {
			foreach ($values as $support) {
				$arrSupport[$support['id']] = $support['name'];
			}
		}
		//edit news
		if ($this->request->is('post')) {
			$params = $this->request->data;
			//lay thong tin nguoi support
			$supportId = $params['LegelAdvices']['support_id'];

			foreach ($supports as $values) {
				foreach ($values as $support) {
					if($support['id'] == $supportId) {
						$params['LegelAdvices']['support_name']  = $support['name'];
						$params['LegelAdvices']['support_skype'] = $support['skype'];
						$params['LegelAdvices']['support_yahoo'] = $support['yahoo'];
						$params['LegelAdvices']['support_phone'] = $support['hotline'];
					}
				}
			}

			$this->LegelAdvices->id = $id;
			if ($this->LegelAdvices->save($params)) {
				return $this->redirect(array('action' => 'index'));
			} else {

			}

		}

		//set view
		$this->set('supports', $arrSupport);
		$this->set('detail', $detail);
		$this->set('listCategory', $this->categorymulti);
		$this->set('title_for_layout', $this->moduleAdmin['legal_advice']['edit']);
	}

	/**
	 * Delete news
	 *
	 * @param string $id
	 */
	public function delete($id = null){
		$this->layout = null;
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}

		$this->LegelAdvices->read('id', $id);
		$this->LegelAdvices->set('del_flg', 1);
		$this->LegelAdvices->save();
		$this->Session->setFlash(__('Delete success'), 'success');
		return $this->redirect(array('action' => 'index'));

	}

	/**
	 * Update news
	 * @param string $id
	 * @param string $type
	 */
	public function update($id = null, $field = 'active'){
		$this->layout = null;
		$this->render(false);
		if ($this->request->is('post')) {
			$value = $this->request->data['value'];
			$this->LegelAdvices->set('id', $id);
			$this->LegelAdvices->set($field, $value);
			$this->LegelAdvices->save();
			echo $value;
		}
	}

	/**
	 * Sap xep danh muc theo level
	 *
	 * @param array $arrayCategory
	 * @param number $keystart nút cha
	 * @param number $level
	 */
	private function _sortLevel($arrayCategory, $keystart = 0, $level = -1){
		//kiểm tra xem tồn tại record không
		if (array_key_exists($keystart, $arrayCategory)) {
			$level++;
			foreach ($arrayCategory[$keystart] as $key => $value){

				//gán các phần tử cho array menu sắp xếp theo đúng vị trí
				//tang so thu tu
				$this->stt++;
				$this->categorymulti[$this->stt] = $value;

				//gan level cho menu
				$this->categorymulti[$this->stt]['level'] = $level;

				//thiet lap de biet day la` 1 nut cha
				if (array_key_exists($key,$arrayCategory)){
					$this->categorymulti[$this->stt]["parent"] = 1;
				} else {
					$this->categorymulti[$this->stt]["parent"] = 0;
				}

				//de quy de lap lai, neu menu_id nam trong array cac menu cha
				$this->_sortLevel($arrayCategory, $key, $level);
			}
		}
	}
}
